﻿/******************************************************    
文件名:operate.cs
版权  (c)  2010-11-23 淘渺圈圈工作室
作者:黄媛媛
创建日期:2010-11-23
维护人员:黄媛媛
维护日期:2010-11-23
文件功能描述:金属材料热处理网络学习平台_访问数据库操作
******************************************************/
using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using System.Data;

namespace Mmht.DB
{
    public class operate
    {
        private SqlConnection conn = new SqlConnection();
        private SqlCommand cmd = new SqlCommand();
        private SqlDataAdapter da = new SqlDataAdapter();
        private string connstr = string.Empty;

        public operate()
        {
            connstr = System.Configuration.ConfigurationManager.AppSettings["connstr"];
            if (connstr == "")
            {
                connstr = "Data Source=.; Initial Catalog=mmht;uid=sa;password=sa123";
            }
        }

        /// <summary>
        /// 数据查询
        /// </summary>
        /// <param name="dt">数据表</param>
        /// <param name="ds">数据集</param>
        /// 所需参数 sql字符串
        /// 用于查询数据集
        /// 返回数据表
        /// ///  Writer：黄媛媛
        ///Create Date：2010-11-29 
        public DataTable Query(string sqlstr)
        {
            conn = new SqlConnection(connstr);
            DataTable dt = null;
            try
            {
                conn.Open();
                da = new SqlDataAdapter(sqlstr, conn);
                DataSet ds = new DataSet();
                da.Fill(ds);
                dt = ds.Tables[0];
                return dt;

            }
            catch (Exception ex)
            {
                //MessageBox.Show(ex.ToString());
                return null;
            }
            finally
            {
                conn.Close();
            }
        }

        /// <summary>
        /// 数据查询
        /// </summary>
        /// <param name="dt">数据表</param>
        /// <param name="ds">数据集</param>
        /// 所需参数 sql字符串
        /// 用于更改数据表，如insert delete update
        /// 返回布尔值
        /// ///  Writer：黄媛媛
        ///Create Date：2010-11-29 
        public bool Execute(string sqlstr)
        {
            conn = new SqlConnection(connstr);
            try
            {
                conn.Open();
                cmd = new SqlCommand(sqlstr, conn);
                cmd.ExecuteNonQuery();
                return true;
            }
            catch (Exception ex)
            {
                //MessageBox.Show(ex.Message, "数据库出错！");
                return false;
            }
            finally
            {
                conn.Close();
            }

        }

       
    }
}